/**
 * 
 */
package cii_functions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cii_lib.CCII_COMPONENT;
import cii_lib.CCII_INTERFACE;
import cii_lib.CCII_TABLE_INDEX;
import cii_lib.CIIdata;
import cii_opto.CCII_IDENTIFICATOR;

/**
 * @author pdrabik
 *
 */
public class FCII_IDENTIFICATOR_old extends CCII_IDENTIFICATOR {
	
	private static final Logger log = LoggerFactory
			.getLogger(FCII_IDENTIFICATOR_old.class);

	/**
	 * @param itab
	 * @param cidx
	 * @param interf
	 * @param parent
	 */
	public FCII_IDENTIFICATOR_old(CCII_TABLE_INDEX itab, int cidx, CCII_INTERFACE interf, CCII_COMPONENT parent) {
		super(itab, cidx, interf, parent);
	}
	
//	public FCII_IDENTIFICATOR_old(CCII_IDENTIFICATOR identificator) {
////		super(identificator.CII_IDENTIFICATOR().getT);
//		
//	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

	/**
	 * 
	 * @param datS Softaware representation of data parameter
	 * @param datH Hardware data stored in register
	 * @return True if match, false if software dont match with hardware values
	 */
	public boolean testCreator(CIIdata datS, CIIdata datH) {
		log.info("datS: {} datH: {}", new Object[] {datS, datH});
		String val = SPAR_CREATOR().Get();
		CIIdata data= new CIIdata(WORD_CREATOR().Width());
		WORD_CREATOR().Read(0,0,data);
		if (datS!=null && datH!=null) {
		datS.WidthSet((""+val).length()*8);
		datS.SetStr(""+val);
		datH.WidthSet((""+data.GetStr()).length()*8);
		datH.SetStr(""+data.GetStr());
		}
		if (val.equals(data.GetStr())) {
			log.debug("Result: {}", Boolean.TRUE);
			return true;
		} else {
			log.debug("Result: {}", Boolean.FALSE);
			return false;
		}
	}
	/**
	 * 
	 * @param datS Softaware representation of data parameter
	 * @param datH Hardware data stored in register
	 * @return True if match, false if software dont match with hardware values
	 */
	public boolean testName(CIIdata datS, CIIdata datH) {
		log.info("datS: {} datH: {}", new Object[] {datS, datH});
		String val = SPAR_NAME().Get();
		CIIdata data= new CIIdata(WORD_NAME().Width());
		WORD_NAME().Read(0,0,data);
		if (datS!=null && datH!=null) {
			datS.WidthSet((""+val).length()*8);
			datS.SetStr(""+val);
			datH.WidthSet((""+data.GetStr()).length()*8);
			datH.SetStr(""+data.GetStr());
			}
			if (val.equals(data.GetStr())) {
				log.debug("Result: {}", Boolean.TRUE);
				return true;
			} else {
				log.debug("Result: {}", Boolean.FALSE);
				return false;
			}
	}
	/**
	 * @param datS Softaware representation of data parameter
	 * @param datH Hardware data stored in register <br>IMPORTANT: datH data is stored as String 
	 * @return True if match, false if software dont match with hardware values
	 */
	public boolean testID(CIIdata datS, CIIdata datH) {
		log.info("datS: {} datH: {}", new Object[] {datS, datH});
		int val = IPAR_IDENTYFIER().Get();
		CIIdata data= new CIIdata(WORD_IDENTYFIER().Width());
		WORD_IDENTYFIER().Read(0,0,data);
		if (datS!=null && datH!=null) {
			datS.WidthSet((""+val).length()*8);
			datS.SetStr(""+val);
			datH.WidthSet((""+data.GetValue()).length()*8);
			datH.SetStr(""+data.GetValue());
			}
		if (val==data.GetValue()) {
			log.debug("Result: {}", Boolean.TRUE);
			return true;
		} else {
			log.debug("Result: {}", Boolean.FALSE);
			return false;
		}
	}
	/**
	 * @param datS Softaware representation of data parameter
	 * @param datH Hardware data stored in register 
	 * <br>IMPORTANT: datH data is stored as String 
	 * @return True if match, false if software dont match with hardware values
	 */
	public boolean testVersion(CIIdata datS, CIIdata datH) {
		log.info("datS: {} datH: {}", new Object[] {datS, datH});
		String val = HPAR_VERSION().Get();
		CIIdata data= new CIIdata(WORD_VERSION().Width());
		WORD_VERSION().Read(0,0,data);
		if (datS!=null && datH!=null) {
			datS.WidthSet((""+val).length()*8);
			datS.SetStr(""+val);
			datH.WidthSet((""+data.GetStr()).length()*8);
			datH.SetStr(""+data.GetStr());
			}
			if (val.equals(data.GetStr())) {
				log.debug("Result: {}", Boolean.TRUE);
				return true;
			} else {
				log.debug("Result: {}", Boolean.FALSE);
				return false;
			}
	}
	/**
	 * Function to perform test all component's services. 
	 * @return True if all services are checked well, false if there is some error.
	 */
	public boolean testAll() {
		return (testVersion(null, null) && testID(null, null)
				&& testCreator(null, null) && testName(null, null) && testUser(null,null));
	}
	
	public boolean testUser(CIIdata datS, CIIdata datH) {
		log.info("datS: {} datH: {}", new Object[] {datS, datH});
		int num = IPAR_USER_REG_NUM().Get();
		int rtest = (int)(Math.random()*WORD_USER().Width()*500);
		CIIdata data= new CIIdata(WORD_USER().Width());
		while(num>0) {
			WORD_USER().Write(num-1,rtest);
			WORD_USER().Read(num-1,data);
			if (datS!=null && datH!=null) {
				datS.WidthSet((""+rtest).length()*8);
				datS.SetStr(""+rtest);
				datH.WidthSet((""+data.GetValue()).length()*8);
				datH.SetStr(""+data.GetValue());
				}	
			if (data.GetValue()!=rtest) {
				log.debug("Result: {}", Boolean.FALSE);
				return false;
			}
			num--;
		}	
		log.debug("Result: {}", Boolean.TRUE);
		return true;
	}
	
	protected boolean writeSample(CIIdata sample) {
		log.info("sample: {}", sample);
		int num = IPAR_USER_REG_NUM().Get();
		CIIdata data= new CIIdata(WORD_USER().Width());
		boolean tmp = true;
		while(num>0) {
			WORD_USER().Write(num-1,sample);
			WORD_USER().Read(num-1,data);
			if (data.GetValue()!=sample.GetValue()) tmp = false;
			num--;
		}	
		log.debug("Result: {}", Boolean.toString(tmp));
		return tmp;
	}
	
	public boolean writeRndSamples(int sampcount, StringBuffer sbuf) {
		log.info("sampcoune: {} sbuf: {}", new Object[] {Integer.toString(sampcount), sbuf});
		long tmp =0;
			for (int i=0;i < sampcount;i++) {
				long rtest = (long)(Math.random()*WORD_USER().Width()*1000);
				if (i==0 && sampcount>4) rtest=0;
				if (i==1 && sampcount>4) rtest=(long)Math.pow(2, WORD_USER().Width())-1; 
				if ((i==2 || i==3)&& sampcount>4) {
					rtest=0;
					for (int j =0;j<WORD_USER().Width()/2;j++) {
						long parz = 2*j;
						rtest = rtest + (long)Math.pow(2, parz);
					}
				}
				if (i==3 && sampcount>4) rtest=rtest^(tmp=(long)Math.pow(2, WORD_USER().Width())-1);   
				CIIdata dc = new CIIdata(WORD_USER().Width());
				dc.SetValue(rtest);
				boolean tm = writeSample(dc);
				if (sbuf!=null) sbuf.append("writen: "+ dc.GetBin0()+ " test: " + (tm?"OK":"WRONG")+"\n");
			}
		log.debug("Result: {}", Boolean.TRUE);
		return true;
	}
	

}
